package top.oylan.manager.controller.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.oylan.manager.entity.*;
import top.oylan.manager.entity.api.ProductApi;
import top.oylan.manager.entity.api.VideoIndexApi;
import top.oylan.manager.entity.api.entity.ProductEntity;
import top.oylan.manager.entity.api.entity.VideoTypeEntity;
import top.oylan.manager.entity.api.result.ProductResult;
import top.oylan.manager.entity.api.result.VideoIndexResult;
import top.oylan.manager.entity.extend.TypesExtend;
import top.oylan.manager.service.CategoryService;
import top.oylan.manager.service.ProductService;
import top.oylan.manager.service.TypeService;
import top.oylan.manager.tools.ApiUtils;
import top.oylan.manager.tools.Pager;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-06-01
 */
@RestController
@RequestMapping("/api/video")
@Api(value = "video", description = "影视接口", produces = MediaType.APPLICATION_JSON_VALUE)
public class ApiVideoController {

    @Resource
    TypeService typeService;

    @Resource
    CategoryService categoryService;

    @Resource
    ProductService productService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    @ApiOperation(value = "影视类型分类接口", response = VideoIndexApi.class, httpMethod = "GET")
    public VideoIndexApi index() {

        VideoIndexApi api = new VideoIndexApi();
        VideoIndexResult result = new VideoIndexResult();
        ArrayList<TypesExtend> typesExtendList = typeService.selectVideoTypeCategory();
        List<VideoTypeEntity> entityList = (List<VideoTypeEntity>) typesExtendList.clone();
        result.setVideoTypeEntityList(entityList);
        ApiUtils.addRightData(api, "成功", result);
        return api;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "影视列表", response = ProductApi.class, httpMethod = "GET")
    public ProductApi getList(
                              @RequestParam(value = "type", defaultValue = "1") byte type,
                              @RequestParam(value = "cateId", defaultValue = "0") int cateId,
                              @RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "rowSize", defaultValue = "10") Integer rowSize) {

        ProductApi api = new ProductApi();
        ProductResult result = new ProductResult();
        Pager pager = new Pager();
        Product condition = new Product();
        condition.setType(type);
        if (cateId > 0) {
            List<Category> categories = categoryService.selectCategoryByType((int) type);
            List<Short> cateIdStr = categoryService.categoryTree(categories,new ArrayList<>(Arrays.asList((short)cateId)), cateId);
            condition.setCateIdStr(cateIdStr);
        }
        pager.setEveyPageRow(rowSize);
        pager.setCurrentPage(page);
        pager.setCondition(condition);
        ArrayList<Video> videos = productService.selectProductByPager(pager);
        List<ProductEntity> productEntities = (List<ProductEntity>) videos.clone();
        result.setProductEntities(productEntities);
        result.setTotalPage(pager.getTotalPage());
        result.setTotalRow(pager.getTotalRow());
        result.setRowSize(pager.getEveyPageRow());
        result.setCurrentPage(pager.getCurrentPage());
        ApiUtils.addRightData(api, "成功", result);
        return api;
    }

}
